package book.ch10;

import java.io.*;

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        File file = new File("2.txt");
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < 3000000; i++) {
            sb.append("abcdefghigklmnopqrstuvwsyz");
        }
        char[] chars = sb.toString().toCharArray();

        long start = System.currentTimeMillis();
        write(file, chars);
        long end = System.currentTimeMillis();

        long start2 = System.currentTimeMillis();
        bufferedWrite(file, chars);
        long end2 = System.currentTimeMillis();

        System.out.println("普通字节流耗时：" + (end - start) + " ms");
        System.out.println("缓冲字节流耗时：" + (end2 - start2) + " ms");

    }

    // 普通字节流
    public static void write(File file, char[] chars) throws IOException {
        FileWriter os = new FileWriter(file);
        os.write(chars);
        os.close();
    }

    // 缓冲字节流
    public static void bufferedWrite(File file, char[] chars) throws IOException {
        BufferedWriter bo = new BufferedWriter(new FileWriter(file));
        bo.write(chars);
        bo.close();
    }
}
